var index_books_order = 1;

$(document).ready(function (){
	view_init();
	event_init();
});

function view_init()
{
	loadingStart();
	
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
	// 查询按钮
	$("#query").click(function (){
		var createTimeStringStart = $("[data-id=createTimeStringStart]").val();
		var createTimeStringEnd = $("[data-id=createTimeStringEnd]").val();
		if (createTimeStringStart && createTimeStringEnd)
		{
			if (moment(createTimeStringStart).isAfter(createTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		$("#list_tbody").html("");
		list_list(true);
	});
	
	
	// 详情按钮
	$("#list_tbody").delegate("[data-id=detail]" ,"click" ,function (){
		var tempId = $(this).parent().attr("data-id");
		if (tempId==null||tempId == undefined||tempId == "")
		{
			$("body").umDialog("alert" ,{"content":"订单编号异常！",left:"40%"});
		}else{
			window.location.href = "/returnorder/direction/detailexpress.do?id="+$(this).parent().attr("data-id");
		}
	});
	
	
	// 翻页事件
	index_initPageEvent("list_list()");
	
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	um_ajax_post(
			INDEX_WEBAPP + "/returnorder/selectExpress.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.returnorder);
			},
			null);
	function _render(data)
	{
		//alert(data[0].author);
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr data-id="returnOrderDetail" >');
				buffer.push('<td style="display:none">'+beautyNull(data[i].id)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].orderNo)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].account)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].quantity)+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].paymode)+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].paystatus)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].createdate)+'</td>');
				buffer.push('<td>'+statusChange(data[i].status)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].expresscode)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].expresscompanyid)+'</td>');
				buffer.push('<td>'+(data[i].remark> 6 ?data[i].remark.substr(0,6) + "..." : data[i].remark )+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].ordertype)+'</td>');
			//	buffer.push('<td>'+(data[i].comment> 6 ?data[i].comment.substr(0,6) + "..." : data[i].comment )+'</td>');
				buffer.push('<td>'+beautyNull(data[i].uid)+'</td>');
				buffer.push('<td>'+(data[i].expressmessage> 6 ?data[i].expressmessage.substr(0,6) + "..." : data[i].expressmessage)+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+beautyNull(data[i].orderNo)+'"><a href="#" data-id=detail> 详情 </td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}
function statusChange(str)
{
	if (str == null || str == undefined)
	{
		return "";
	}
	else if (str == "102")
	{
		// 还书单快递员更新了快递信息
		return "快递更新";
	}
	else if (str == "997")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "发起还书";
	}
	else if (str == "996")
	{
		// 借书单确认初始状态（待审核订单）：998
		return "完成还书";
	}
	else
	{
		// 状态码所代表含义未知
		return "未知状态";
	}
	return str;
}
